<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_user {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user WHERE user_type = 2";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function save($user_id, $email, $password, $user_type,
            $first_name, $last_name, $address, $phone) {
        $db = new connection();
        $con = $db->open_connect();

        if ($user_id == 0) {
            $query = "INSERT INTO tbl_user(email, password, user_type,
                first_name, last_name, address, phone) VALUES (
                '" . $email . "','" . md5($password) . "'," . $user_type . ",'" . $first_name . "','" . $last_name . "','" . $address . "','" . $phone . "')";
        } else {
            $query = "UPDATE tbl_user SET "
                    . "email = '" . $email . "',"
                    . "password = '" . $password . "',"
                    . "user_type = " . $user_type . ","
                    . "first_name = '" . $first_name . "',"
                    . "last_name = '" . $last_name . "',"
                    . "address = '" . $address . "',"
                    . "phone = '" . $phone . "' "
                    . "WHERE user_id = " . $user_id;
        }
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user WHERE user_type = 2 AND user_id = " . $id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_user WHERE user_id = " . $id;
        /** @noinspection PhpParamsInspection */
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_login_info($email, $password) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user JOIN tbl_user_type on user_type = user_type_id "
                . "WHERE email = '" . $email . "' "
                . "AND password = '" . md5($password) . "'";
        $result = mysqli_query($con, $query);
        if (!$result) {
            printf("Error: %s\n", mysqli_error($con));
            exit();
        }

        return mysqli_fetch_array($result);
    }

    public function get_by_email($email) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user WHERE user_type = 2 AND email = '".$email."'";
        $result = mysqli_query($con, $query);
        if (!$result) {
            printf("Error: %s\n", mysqli_error($con));
            exit();
        }

        return mysqli_fetch_array($result);
    }

    public function update_password($email, $password_new) {
        $db = new connection();
        $con = $db->open_connect();
        $query = "UPDATE tbl_user SET "
                . "password = '" . md5($password_new) . "' "
                . "WHERE email = '" . $email . "'";

        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
}
